package com.action.gulimall.search.controller;

import brave.Tracer;
import com.action.common.utils.R;
import com.action.gulimall.search.feign.ProductFeignService;
import com.action.gulimall.search.service.MallSearchService;
import com.action.gulimall.search.vo.SearchParam;
import com.action.gulimall.search.vo.SearchResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author sl shilun217@qq.com
 * @Deacription 搜索业务
 * @date 2021/06/22/17:37
 **/
@Slf4j
@Controller
public class SearchController{
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    MallSearchService mallSearchService;
    @Autowired
    ProductFeignService productFeignService;
    @Autowired
    Tracer tracer;
    @GetMapping("/list.html")
    public String list(SearchParam searchParam, Model model) throws IOException {
        log.info("开始进行商品搜索------------");
        tracer.currentSpan().tag("SearchController.list", "开始进行商品搜索");
        SearchResult searchResult = mallSearchService.search(searchParam);
        model.addAttribute("result", searchResult);
        redisTemplate.opsForValue().set("testkey", "1", 2000, TimeUnit.MICROSECONDS);
        return "list";
    }
    @ResponseBody
    @GetMapping("/feign/{attrId}")
    public R feigntest(@PathVariable(value="attrId") Long attrId){
        return productFeignService.attrInfo(attrId);
    }
    @ResponseBody
    @GetMapping("/hello")
    public R hello(){
        redisTemplate.opsForValue().set("testkey", "1", 2000, TimeUnit.MICROSECONDS);
        return R.ok();
    }

}
